Preparing the data

### Notes
# install.packages('ggplot2',dependencies = T)
# Test code line with command+enter
# update.packages(checkBuilt = T,ask = F)
# head(ds) # first few lines of a dataframe
# str(ds) #show structure of dataframe
###

# Setting variables:
options(width=108)
outT <- 2.5 # Outlier factor

# Loading libraries
library(ggplot2) # for plotting
library(plyr)

#Importing the data
#data_dir <- '/Volumes/PK/'
homeDir <- path.expand('~')
dataDir <- paste(homeDir, 'Dropbox/Projects/fc/fc/data/usable/extracted', sep='/')
dataFiles <- dir(dataDir, pattern='csv')
nSubj <- length(dataFiles) # number of subjects

# Going through all subjects' data:
df <- data.frame() # data frame that combines all subjects' data into a single ds
curSubjN <- 6 #temp

for(curSubjN in 1:nSubj){
    # Loading the data;
    ds <- read.csv(paste(dataDir, dataFiles[curSubjN], sep='/'))
    # Shorter names for variables:
    colnames(ds) <- c('subjId', 'domEyeR', 'threshStHi', 'threshStLo', 'thresh', 'trialN', 'sentId',
                      'sentPx', 'congr', 'fam', 'locTop', 'cued', 'corr', 'broken', 'st')
    # Non-blank data set:
    cds <- ds[ds$sentId<31,]
    # Removing the outliers:
    outlLo <- cds$st>(mean(cds$st)-outT*sd(cds$st)) # binary for every row
    outlHi <- cds$st<(mean(cds$st)+outT*sd(cds$st))
    cds$outlNlo <- sum(!outlLo) # counting the number of outliers before removing
    cds$outlNhi <- sum(!outlHi)
    cds <- cds[outlLo,] # removing the outliers
    cds <- cds[outlHi,]
    cds <- cds[!is.na(cds$subjId),] # usually don't need this; remove NA rows
    # Normalized suppression times:
    cds$stNorm <- cds$st / mean(cds$st)    
    # Binding the subject ds to the common data frame:
    df <- rbind(df, cds)
}
# Prettying up the data frame:
df$subjId <- factor(df$subjId)
df$Familiarity[df$fam==1] <- 'Familiar\n(Upright)'
df$Familiarity[df$fam==0] <- 'Unfamiliar\n(Inverted)'
df$Congruency[df$congr==1] <- 'Congruent'
df$Congruency[df$congr==0] <- 'Incongruent'
df$Attention[df$cued==1] <- 'Cued'
df$Attention[df$cued==0] <- 'Uncued'
# Binning
df$bin <- 6
df$bin[df$trialN<500] <- 5
df$bin[df$trialN<400] <- 4
df$bin[df$trialN<300] <- 3
df$bin[df$trialN<200] <- 2
df$bin[df$trialN<100] <- 1
head(df)
##   subjId domEyeR threshStHi threshStLo  thresh trialN sentId sentPx congr fam locTop cued corr broken
## 1      1       1    0.24862     0.2398 0.24421      1     10    124     1   0      0    0    1      1
## 2      1       1    0.24862     0.2398 0.24421      2     15    121     0   1      1    1    1      1
## 3      1       1    0.24862     0.2398 0.24421      3     28    100     0   0      0    0    1      1
## 4      1       1    0.24862     0.2398 0.24421      4     28    100     0   0      1    1    1      1
## 5      1       1    0.24862     0.2398 0.24421      5      2    133     0   1      0    0    1      1
## 6      1       1    0.24862     0.2398 0.24421      6     12    117     0   0      1    1    1      1
##       st outlNlo outlNhi    stNorm            Familiarity  Congruency Attention bin
## 1 1.1426       1       7 0.8461978 Unfamiliar\n(Inverted)   Congruent    Uncued   1
## 2 1.7793       1       7 1.3177312    Familiar\n(Upright) Incongruent      Cued   1
## 3 1.3451       1       7 0.9961672 Unfamiliar\n(Inverted) Incongruent    Uncued   1
## 4 1.4134       1       7 1.0467494 Unfamiliar\n(Inverted) Incongruent      Cued   1
## 5 1.5021       1       7 1.1124397    Familiar\n(Upright) Incongruent    Uncued   1
## 6 1.3269       1       7 0.9826884 Unfamiliar\n(Inverted) Incongruent      Cued   1

Quality control

Summary stats per participant

ddply(df, c('subjId'), summarise, outlNumLow = median(outlNlo), outlNumHigh = median(outlNhi),
      cuedCaught = sum(cued & corr), uncuedCaught = sum(!cued & corr))
##    subjId outlNumLow outlNumHigh cuedCaught uncuedCaught
## 1       1          1           7        112          115
## 2      14          0           7        116          116
## 3      17          0           9        115          116
## 4      19          0          10        113          107
## 5      21          0           6        118          116
## 6      23          0           6        117          116
## 7      24          0           0        105          101
## 8      25          0           9        115          116
## 9      26          0           9        118          113
## 10     27          0          11        110          115
## 11     28          1           7        115          116
## 12     29          0          14        114          112
## 13     30          0           5        118          117
## 14     31          0           7        116          115
## 15     32          0           7        119          114
## 16     33          0          11        107          104

sts across trials

for(curSubjN in 1:nSubj){
    curSubjId <- levels(df$subjId)[curSubjN]
    ss <- df[df$subjId==curSubjId,]
    p <- ggplot(data=ss, aes(x=trialN, y=st)) +  geom_line() + theme_bw() + ylim(.5,2) +
        labs(x='Trial Number', y='Suppression Time', title=paste('Participant', as.character(curSubjId))) + 
        theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
              axis.text=element_text(size=8), axis.title=element_text(size=9),
              legend.text=element_text(size=8), legend.title=element_text(size=9),
              legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
              legend.background = element_rect(fill='transparent'))
    plot(p)
}

## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_path).

Individual plots

Congruence X Familiarity X Cue

for(curSubjN in 1:nSubj){
    curSubjId <- levels(df$subjId)[curSubjN]
    ss <- df[df$subjId==curSubjId,]
    p <- ggplot(data=ss, aes(x=factor(Familiarity), y=st, colour=factor(Congruency))) +  geom_boxplot() + 
        facet_grid(~Attention) + theme_bw() +
        labs(x='Familiarity', y='Suppression Time', colour='Congruency',
             title=paste('Participant', as.character(curSubjId))) + 
        theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
              axis.text=element_text(size=8), axis.title=element_text(size=9),
              legend.text=element_text(size=8), legend.title=element_text(size=9),
              legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
              legend.background = element_rect(fill='transparent'))
    plot(p)
}

Group plots

Is cueing effective across participants?

p <- ggplot(data=df, aes(x=Attention, y=st)) + geom_boxplot() + facet_grid(~subjId) + theme_bw() +
    labs(x='Attention effect X Subject', y='Suppression Time', colour='Attention',
         title='Effect of attentional cueing') +
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(.0, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

Is cueing effective across trials?

p <- ggplot(data=df, aes(x=Attention, y=st)) + geom_boxplot() + facet_grid(~bin) + theme_bw() +
    labs(x='Attention effect X 100-trial bins', y='Suppression Time', colour='Attention',
         title='Effect of attentional cueing') +
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(.0, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

Congruence X Familiarity X Cue

dfSum <- ddply(df, .(subjId, Attention, Familiarity, Congruency), summarise, 
               `Mean Normalized ST` = mean(stNorm, na.rm=T))
p <- ggplot(data=dfSum, aes(x=factor(Familiarity), y=`Mean Normalized ST`, 
                            colour=factor(Congruency))) + 
    geom_boxplot() +  facet_grid(~Attention) + theme_bw() +
    labs(x='Familiarity', y='Suppression Time', colour='Congruency',
         title='Congruence X Familiarty X Cue') + 
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

p <- ggplot(data=dfSum, aes(x=factor(Familiarity), y=`Mean Normalized ST`, 
                            group=factor(subjId), colour=factor(subjId))) + 
    geom_line() + facet_grid(~Attention*Congruency) + theme_bw() +
    labs(x='Familiarity', y='Suppression Time', colour='Congruency',
         title='Congruence X Familiarty X Cue') + 
    theme(panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(),
          axis.text=element_text(size=8), axis.title=element_text(size=9),
          legend.text=element_text(size=8), legend.title=element_text(size=9),
          legend.key = element_blank(), #legend.margin=unit(-.04, 'in'),
          legend.background = element_rect(fill='transparent'))
plot(p)

Linear models

# Centered data set: